package com.burt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.burt.dao.EmployeeRepository;
import com.burt.domain.Employee;

/**
 * Description: User: Burt Date: 2018-08-26 Time: 22:00
 */
@RestController
@RequestMapping("/es")
public class EsController {

    private final EmployeeRepository employeeRepository;

    @Autowired
    public EsController(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }

    @RequestMapping("/add/{id}")
    public String addEmployee(@PathVariable String id) {
        Employee employee = new Employee();
        employee.setId(id);
        employee.setAbout("about" + id);
        employee.setAge(18);
        employee.setFirstName("F");
        employee.setLastName("L");
        employeeRepository.save(employee);

        System.out.println("add a employee: " + id);

        return "success";
    }

    @RequestMapping("/delete/{id}")
    public boolean deleteEmployee(@PathVariable String id) {
        employeeRepository.deleteById(id);
        return true;
    }

    @RequestMapping("/update/{id}")
    public boolean updateEmployee(@PathVariable String id) {
        Employee employee = employeeRepository.queryEmployeeById(id);
        employee.setFirstName("modify" + id);
        employeeRepository.save(employee);

        System.out.println("update employee: " + id);

        return true;
    }

    @RequestMapping("/query/{id}")
    public Employee queryEmplyee(@PathVariable String id) {
        Employee employee = employeeRepository.queryEmployeeById(id);
        return employee;
    }
}
